package object;

public class Test {
    //基于链表实现队列
    static class Node {
        public String val;
        public Node next;
        public Node(String val) {
            this.val = val;
            this.next = null;
        }
    }
    private Node head = null;
    private Node tail = null;
    public void offer(String val){
        Node newNode = new Node(val);
        if(head == null) {
            head = newNode;
            tail = newNode;
            return;
        }
        tail.next = newNode;
        newNode.next = null;
        tail = newNode;
    }
    public String poll(){

        if(head == null) {
            return null;
        }
        String val = head.val;
        if(head.next == null) {
            head = null;
            tail = null;
            return val;
        }
        head = head.next;
        return val;
    }
    public String peek(){
        if(head == null) {
            return null;
        }
        return head.val;
    }
    public int size(){
        int size = 0;
        for(Node cur = head; cur != null; cur = cur.next) {
            size++;
        }
        return size;
    }
    public boolean isEmpty(){
        return head == null;
    }

    public String toString(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for(Node cur = head; cur != null; cur = cur.next) {
            stringBuilder.append(cur.val);
            if(cur.next != null) {
                stringBuilder.append(", ");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.offer("1");
        test.offer("2");
        test.offer("3");
        test.offer("4");
        System.out.println(test);
        System.out.println(test.size());
        System.out.println(test.poll());
        System.out.println(test.poll());
        System.out.println(test);
        System.out.println(test.size());
        System.out.println(test.peek());
        System.out.println(test.isEmpty());

    }


}
